setwd("~/Box Sync/Replication_AJPS_0215/Figure_2")
library(foreign)
value<-read.dta("ValueAdded_0712.dta")
value07<-subset(value, select=c("DistrictIRN","AllGrade200607ValueAddedCom"))
names(value07)<-c("DistrictIRN","Value")
value07$FY<-2007
value08<-subset(value, select=c("DistrictIRN","AllGrade200708ValueAddedCom"))
names(value08)<-c("DistrictIRN","Value")
value08$FY<-2008
value09<-subset(value, select=c("DistrictIRN","AllGrade200809ValueAddedCom"))
names(value09)<-c("DistrictIRN","Value")
value09$FY<-2009
value10<-subset(value, select=c("DistrictIRN","AllGrade200910ValueAddedCom"))
names(value10)<-c("DistrictIRN","Value")
value10$FY<-2010
value11<-subset(value, select=c("DistrictIRN","AllGrade201011ValueAddedCom"))
names(value11)<-c("DistrictIRN","Value")
value11$FY<-2011
value12<-subset(value, select=c("DistrictIRN","AllGrade201112ValueAddedCom"))
names(value12)<-c("DistrictIRN","Value")
value12$FY<-2012
value<-rbind(value07, value08, value09, value10, value11, value12)
value$DistrictIRN<-as.numeric(value$DistrictIRN)
write.dta(value, "140219_valueadded.dta")

ayp<-read.dta("DistrictLRCandAYP_FY02FY11_0214.dta")
ayp<-subset(ayp, select=c("Dist_IRN","FY","dist_OverallAYP"))
ayp<-subset(ayp, dist_OverallAYP!="")
ayp$dist_OverallAYP[ayp$dist_OverallAYP=="Met"]<-"AYP Met"
ayp$dist_OverallAYP[ayp$dist_OverallAYP=="Not Met"]<-"AYP Not Met"
ayp<-merge(ayp, value, by.x=c("Dist_IRN","FY"),by.y=c("DistrictIRN","FY"))

ind<-read.dta("AYPMet_ProfMathRead_Indicator.dta")

ayp<-merge(ayp, ind, by=c("Dist_IRN","FY"), all.x=TRUE)
library(ggplot2)
library(scales)

chisq.test(table(ayp$dist_OverallAYP,ayp$Value))

table(ayp$dist_OverallAYP,ayp$Value)

ayp$ayp_ord<-NA
ayp$ayp_ord[ayp$dist_OverallAYP=="AYP Not Met"]<-0
ayp$ayp_ord[ayp$dist_OverallAYP=="AYP Met"]<-1

ayp$va_ord<-NA
ayp$va_ord[ayp$Value=="Below"]<-0
ayp$va_ord[ayp$Value=="Met"]<-1
ayp$va_ord[ayp$Value=="Above"]<-2
library(vcdExtra)
table(ayp$ayp_ord,ayp$va_ord)
GKgamma(table(ayp$ayp_ord,ayp$va_ord))



ayp$Value<-factor(ayp$Value,levels=c("Below","Met","Above"))
pdf("Figure_2.pdf", width=6, height=2)
ggplot(ayp,aes(x = dist_OverallAYP,fill = Value)) + 
    geom_bar(position = "fill", colour="black") + 
    scale_y_continuous(labels = percent_format())+coord_flip()+
   ylab("Percent of District-Years") + xlab("")+
   scale_fill_manual(values=c("black","gray32","gray80"),breaks=c("Below","Met","Above"), name="Value-Added\nDesignation")

dev.off()

hold<-subset(ayp, dist_OverallAYP=="AYP Not Met" & (Value=="Met" | Value=="Above"))
hold2<-subset(ayp, dist_OverallAYP=="AYP Not Met" & Value=="Below")

hold3<-subset(ayp, dist_OverallAYP=="AYP Not Met")
hold4<-subset(ayp, AYPprofMet_MathReadOverall==1)